<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>首页-编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
</head>
<body class="childrenBody">
<form id="formUpdate" class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red">*</span>电脑品牌</label>
        <div class="layui-input-inline typeSelect">
            <input id="typeName" type="hidden" value="${computer.categoryId}">
            <select name="categoryId" id="categoryId">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red"></span>系列</label>
        <div class="layui-input-inline">
            <input type="text" id="item" name="item" class="layui-input" value="${computer.item}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red"></span>标题</label>
        <div class="layui-input-inline">
            <input type="text" id="title" name="title" class="layui-input" value="${computer.title}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red"></span>卖点</label>
        <div class="layui-input-inline">
            <input type="text" id="point" name="point" class="layui-input" value="${computer.point}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red"></span>单价</label>
        <div class="layui-input-inline">
            <input type="text" id="price" name="price" class="layui-input" value="${computer.price}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red"></span>库存</label>
        <div class="layui-input-inline">
            <input type="text" id="num" name="num" class="layui-input" value="${computer.num}">
        </div>
    </div>

    <div class="layui-form-item">
        <label  class="layui-form-label">图片</label>
        <div class="layui-col-md3 layui-col-xs5">
            <div class="layui-upload-list thumbBox mag0 magt3">
                <img class="layui-upload-img thumbImg" src="/${computer.pictureUrl}">
            </div>
        </div>
        <input type="hidden" id="pictureId" name="pictureId" >
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <button  class="layui-btn" type="button" lay-filter="update" lay-submit="">修改</button>
    </div>
    <input  type="hidden" id="id" name="id" value="${id}">

</form>

</body>
</html>
<script src="${ctxPath}/static/manage/layui/layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/manage/js/x-layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/manage/js/jquery.js"></script>

<script type="text/javascript">
    layui.use(['form','layer','upload'], function(){
        var $ = layui.$, form = layui.form, upload = layui.upload, layer = layui.layer;

        // 上传缩略图
        upload.render({
            elem: '.thumbBox',
            size: 600,
            url: '/kaptcha/uploadImg',

            before: function(obj){
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                layer.load();
                obj.preview(function(index, file, result){
                    $('.thumbImg').attr('src', result);
                });
            },
            done: function(res){
                // alert("图片上传成功吗，保存后生效");
                layer.closeAll('loading');
                $("#pictureId").val(res.data);
                $('.thumbBox').css("background","#fff");
            }
        });

        //回显类型信息
        $(function () {
            var typeName = $("#typeName").val();
            $.ajax({
                type: 'post',
                url: '/manage/computer/computerType',
                success:function(res){
                    if (res.length > 0) {
                        for (var i = 0; i < res.length; i++) {

                            if (typeName == res[i].id) {
                                $("#categoryId").append("<option value='" + res[i].id+ "' selected='selected'>" + res[i].type+ "</option>");
                                form.render();
                            } else {
                                $("#categoryId").append("<option value='" + res[i].id+ "'>" + res[i].type + "</option>");
                                form.render();
                            }
                        }
                    }
                }
            });
        })


        // 监听提交
        form.on('submit(update)', function(){
            var pictureId = $("#pictureId").val();
            var price=$("#price").val();
            var num=$("#num").val();

            if(pictureId.length <= 0){
                alert("没有选择图片,或图片上传失败");
                return false;
            }

            if($("#categoryId").val()=="" || $("#categoryId").val().length<0){
                alert("电脑系列没有选择");
                return false;
            }
            if(isNaN(price)){
                alert("请输入正确的格式")
                return false;
            }
            if(isNaN(num)){
                alert("请输入正确的格式")
                return false;
            }

            $.ajax({
                url:'/manage/computer/computerUpdate',
                type:'post',

                data:$("#formUpdate").serialize(),
                success:function(result){
                    alert(result);
                    window.parent.location.reload();
                },
                error:function(result){
                    alert("失败？请及时告知开发人员"+result);
                    window.parent.location.reload();
                }
            });
        });

    });

</script>
